import { combineReducers } from 'redux'

const initialState = {
    userInfo: {
        user_role: {},
        user_expert: {}
    },
};

export const actionsTypes = {
    GET_USER_INFO:"GET_USER_INFO",
    RESPONSE_USER_INFO: "RESPONSE_USER_INFO",
    REAL_NAME_CERTIFICATION: "REAL_NAME_CERTIFICATION",
    CHANG_MOBILE: "CHANGE_MOBILE",
    UPDATE_NICKNAME: "UPDATE_NICKNAME",
    UPDATE_USER: "UPDATE_USER",
    BIND_WECHAT: "BIND_WECHAT",
    BIND_INVITECODE: "BIND_INVITECODE",
    UNBIND_WECHAT: "UNBIND_WECHAT",
    GET_INVITER_INFO: "GET_INVITER_INFO",
    RESPONSE_INVITER_INFO: "RESPONSE_INVITER_INFO",

    SAVE_USER_ROLE: "SAVE_USER_ROLE"
};

export const actions = {
    get_user_info: function () {
        return {
            type: actionsTypes.GET_USER_INFO
        }
    },
    real_name_certification: function (data) {
        return {
            type: actionsTypes.REAL_NAME_CERTIFICATION,
            data
        }
    },
    change_mobile: function (mobile, vcode) {
        return {
            type: actionsTypes.CHANG_MOBILE,
            mobile,
            vcode
        }
    },
    update_user: function (data) {
        return {
            type: actionsTypes.UPDATE_USER,
            data
        }
    },
    bind_wechat: function () {
        return {
            type: actionsTypes.BIND_WECHAT
        }
    },
    unbind_wechat: function () {
        return {
            type: actionsTypes.UNBIND_WECHAT
        }
    },
    bind_invite_code: function (invite_code) {
        return {
            type: actionsTypes.BIND_INVITECODE,
            invite_code
        }
    },
    get_inviter_info: function (invite_code) {
        return {
            type: actionsTypes.GET_INVITER_INFO,
            invite_code
        }
    },
    save_user_role: function (data) {
        return {
            type: actionsTypes.SAVE_USER_ROLE,
            data
        }
    }
};

export function reducer(state = initialState, action) {
    switch (action.type) {
        case actionsTypes.RESPONSE_USER_INFO:
            action.data.user_role =  action.data.user_role.reduce( function(acc, cur, i) {
                acc[cur.code] = cur
                return acc
            }, {} )
            console.log(action.data.user_role)
            return {
                ...state, userInfo: action.data
            }; 
        case actionsTypes.RESPONSE_INVITER_INFO:
            return {
                ...state,
                inviter: action.data

            }     
        default:
            return state
    }
}